<?php

namespace app\index\controller;
use app\index\validate\LogValidate;
use app\BaseController;
use app\index\model\PhotoLogModel;

class PhotoLog extends BaseController
{
    protected $middleware = [
        'Auth' => ['except' => []]
    ];

    public function list()
    {
        $params = $this->request->post(['current', 'size', 'categoryId']);
        $sql = new PhotoLogModel();
        $whereArr[] = ['user_id', '=', $this->request->user->id];
        $haswhereArr[] = ["status", "=", "SUCCESS"];
        $condition = isset($params['categoryId']) && $params['categoryId'];
        if($condition) {
            $haswhereArr[] = ["type_id", "=", $params['categoryId']];
        }
        $list = $sql
            ->where($whereArr)
            ->hasWhere('info', $haswhereArr)
//            ->when($condition, function ($query) use($haswhereArr) {
//                $query->hasWhere('info', $haswhereArr);
//            })
            ->with(['infos'])
            ->order('update_at desc')
            ->paginate([
                'list_rows'=> $params['size'],
                'page' => $params['current'],
            ])->toArray();
        foreach ($list['data'] as $key => $item) {
            $list['data'][$key]['info'] = [
                'author' => [
                    'avatar' => $item['infos']['author']['avatar'],
                    'username' => $item['infos']['author']['username'],
                ],
                'name' => $item['infos']['name'],
                'cover' => $item['infos']['cover'],
                'photo_count' => $item['infos']['photo_count'],
                'video_count' => $item['infos']['video_count'],
                'book_type' => $item['infos']['book_type'],
                'price' => $item['infos']['price'],
                'id' => $item['infos']['id'],
                'status' => $item['infos']['status'],
            ];
            unset($list['data'][$key]['infos']);
        }
        return result()->success($list);
    }
    public function add() {
        $params = $this->request->post(['photo_id', 'menu_id', 'image_id']);
        $validate = new LogValidate();
        if (!$validate->scene('add')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new PhotoLogModel();
        $info = $sql->where('photo_id', $params['photo_id'])->where('user_id', $this->request->user->id)->findOrEmpty();
        if($info->isEmpty()) {
            $params['user_id'] = $this->request->user->id;
            $sql->save($params);
        } else {
            $params['update_at'] = date('Y-m-d H:i:s');
            $info->save($params);
        }
        return result()->success("记录成功");
    }

    public function getLog()
    {
        $params = $this->request->post(['photo_id']);
        $sql = new PhotoLogModel();
        $info = $sql->where('photo_id', $params['photo_id'])->where('user_id', $this->request->user->id)->find();
        return result()->success($info);
    }

    public function getNewLog()
    {
        $sql = new PhotoLogModel();
        $haswhereArr[] = ["book_type", "=", 1];
        $info = $sql
            ->where('user_id', $this->request->user->id)
            ->hasWhere('info', $haswhereArr)
            ->with(['info', 'menu'])
            ->order('update_at desc')
            ->find();
        return result()->success($info);
    }

    public function dels()
    {
        $params = $this->request->post(['ids']);
        $sql = new PhotoLogModel();
        $count = $sql->where('user_id', $this->request->user->id)->where('id', 'in', $params['ids'])->count();
        if($count != count($params['ids'])) {
            return result()->onlyFailMsg("检测到非法操作");
        }
        $sql->destroy($params['ids']);
        return result()->onlySucMsg("删除成功");
    }
}